#!/usr/bin/bash
set -e

# 安装 unzip
apt-get install -y unzip

# 获取安装版本
echo -n "install rocketmq version(5.1.3): "
read version

download_path="https://mirrors.tuna.tsinghua.edu.cn/apache/rocketmq/${version}/rocketmq-all-${version}-bin-release.zip"
binary_name=`basename ${download_path}`

package_dir="/srv/package/"
install_dir="/srv/software/"
custom_env_file="/etc/profile.d/custom_env.sh"

# 创建存放目录，然后下载、安装、改名
mkdir -p ${package_dir} ${install_dir}
wget ${download_path} -P ${package_dir}
unzip ${package_dir}${binary_name} -d ${install_dir}
mv ${install_dir}rocketmq-all-${version}-bin-release ${install_dir}rocketmq-${version}

# 添加环境变量
software_name=$(ls -l ${install_dir} |grep rocketmq |grep ${version} |awk '{print $9}')
cat >> ${custom_env_file} <<-EOF
export ROCKETMQ_HOME=${install_dir}${software_name}
export PATH=\$ROCKETMQ_HOME/bin:\$PATH
EOF
source /etc/profile

# 安装成功提示
echo "Successfully installed rocketmq..."